package com.ssyc.gsk_teacher.adapter;

import android.app.Activity;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.makeramen.roundedimageview.RoundedImageView;
import com.ssyc.common.adapter.CommonAdapter;
import com.ssyc.common.adapter.ViewHolder;
import com.ssyc.common.glide.ImageUtil;
import com.ssyc.common.http.HttpAdress;
import com.ssyc.gsk_teacher.R;
import com.ssyc.gsk_teacher.bean.StudentInfo;

import java.util.List;

/**
 * 用于展示小组成员列表的适配器
 */

public class TeamGvAdapter extends CommonAdapter<StudentInfo> {

    private Context context;

    private Activity activity;

    public TeamGvAdapter(Activity activity, Context context, List<StudentInfo> datas, int layoutId) {
        super(context, datas, layoutId);
        this.context = context;
        this.activity = activity;
    }

    @Override
    public void convert(ViewHolder holder, StudentInfo info) {
        int pos = holder.getPosition();

        RelativeLayout rlOut = holder.getView(R.id.rl_out);


        //点击事件
        rlOut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (onOutClickListener != null) {
                    onOutClickListener.onOUtclick(pos, info);
                }
            }
        });

        //长按事件
        rlOut.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View view) {

                if (TextUtils.isEmpty(info.getName())) {
                    //该位置没有学生时不响应长按事件
                    return false;
                } else {
                    if (onOutLongClickListener != null) {
                        onOutLongClickListener.onLongClick(pos, info);
                    }
                    return true;
                }

            }
        });


        //头像
        RoundedImageView ivHead = holder.getView(R.id.iv_head);

        //姓名
        TextView tvName = holder.getView(R.id.tv_name);
        //根据位置和当前位置是否有名字来确定显示的名字

        if (pos == 0) {
            //设置姓名
            if (TextUtils.isEmpty(info.getName())) {
                tvName.setText("智");
            } else {
                tvName.setText(info.getName());
            }

            //设置头像
            if (!TextUtils.isEmpty(info.getIcon())) {
                //需要拼接URL前缀

                String headUrl = HttpAdress.HEADIMGURL + info.getIcon() + ".jpg";

                ImageUtil.displayImage(context, ivHead, headUrl);
            } else {
                ivHead.setImageResource(R.drawable.teacher_zhi);
            }
        } else if (pos == 1) {
            if (TextUtils.isEmpty(info.getName())) {
                tvName.setText("勇");
            } else {
                tvName.setText(info.getName());
            }

            //设置头像
            if (!TextUtils.isEmpty(info.getIcon())) {
                String headUrl = HttpAdress.HEADIMGURL + info.getIcon() + ".jpg";
                ImageUtil.displayImage(context, ivHead, headUrl);
            } else {
                ivHead.setImageResource(R.drawable.teacher_yong);
            }

        } else if (pos == 2) {

            if (TextUtils.isEmpty(info.getName())) {
                tvName.setText("欢");
            } else {
                tvName.setText(info.getName());
            }

            if (!TextUtils.isEmpty(info.getIcon())) {
                //需要拼接URL前缀
                String headUrl = HttpAdress.HEADIMGURL + info.getIcon() + ".jpg";
                ImageUtil.displayImage(context, ivHead, headUrl, R.drawable.base_default_head);
            } else {
                ivHead.setImageResource(R.drawable.teacher_huan);
            }

        } else if (pos == 3) {
            if (TextUtils.isEmpty(info.getName())) {
                tvName.setText("爱");
            } else {
                tvName.setText(info.getName());
            }

            //设置头像
            if (!TextUtils.isEmpty(info.getIcon())) {
                //需要拼接URL前缀
                String headUrl = HttpAdress.HEADIMGURL + info.getIcon() + ".jpg";
                ImageUtil.displayImage(context, ivHead, headUrl, R.drawable.base_default_head);
            } else {
                ivHead.setImageResource(R.drawable.teacher_ai);
            }
        }


        //删除按钮
        ImageView ivDelete = holder.getView(R.id.iv_delete);
        if (info.isShowDelete()) {
            ivDelete.setVisibility(View.VISIBLE);
        } else {
            ivDelete.setVisibility(View.GONE);
        }

        //删除按钮的点击事件,弹框是否进行删除的询问框
        ivDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
            }
        });
    }

    private OnOutClickListener onOutClickListener;

    public void setOnOutClickListener(OnOutClickListener onOutClickListener) {
        this.onOutClickListener = onOutClickListener;
    }

    /**
     * 点击事件的回调接口
     */
    public interface OnOutClickListener {
        void onOUtclick(int pos, StudentInfo info);
    }


    private OnOutLongClickListener onOutLongClickListener;

    public void setOnOutLongClickListener(OnOutLongClickListener onOutLongClickListener) {
        this.onOutLongClickListener = onOutLongClickListener;
    }

    /**
     * 长按事件的回调接口
     */
    public interface OnOutLongClickListener {
        void onLongClick(int pos, StudentInfo info);
    }
}
